package com.joy.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joy.eduservice.entity.EduSubject;
import com.joy.eduservice.entity.excel.SubjectData;
import com.joy.eduservice.entity.subject.OneSubject;
import com.joy.eduservice.entity.subject.TowSubject;
import com.joy.eduservice.listener.SubjectExcelListener;
import com.joy.eduservice.mapper.EduSubjectMapper;
import com.joy.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-30
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            //文件的输入流
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //课程分类列表（树形）

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1、查询多有的一级分类 parent_id=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);//不用将mapper注入

        //2、查询出所有的二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //因为得到的集合不是想要的，比想要的属性多，只需要里面的几个属性封装就好
        ArrayList<OneSubject> oneSubjects = new ArrayList<>();

        //3、封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每一级分类对象，获取每一个分级对象值
        //封装到要求的list集合里面List<oneSubjectList> finalSubjectList
        for(int i=0;i<oneSubjectList.size();i++){
            //获取到oneSubjectList中的每个对象
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //以上的两句可以用一句代替
            BeanUtils.copyProperties(eduSubject,oneSubject);
            //创建封装二级分类的集合
            ArrayList<TowSubject> towSubjects = new ArrayList<>();
            for(int j=0;j<twoSubjectList.size();j++){
                //获取二级分类集合里面的对象
                EduSubject eduTowSubject = twoSubjectList.get(j);
                if(eduSubject.getId().equals(eduTowSubject.getParentId())){
                    TowSubject towSubject = new TowSubject();
                    towSubject.setId(eduTowSubject.getId());
                    towSubject.setTitle(eduTowSubject.getTitle());
                    towSubjects.add(towSubject);
                }
            }
            //将所有查询出来的二级分类，封装到对应的一级分类的children中
            oneSubject.setChildren(towSubjects);

            oneSubjects.add(oneSubject);

        }
        return oneSubjects;
    }
}
